package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import aes.general.AdminControl;

/**
  * This panel allows the admin to see all the exam time 
  * extend requests that were sent to him. 
  * The admin is able to decline the request or to approve it.
  * If the request is approved the exam time changes immediately
  * for all the students that are making the specific exam.  
  */
public class AdminReqPanel extends JPanel {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel l;
	private JButton b2;
	private JButton b3;
	private JButton b4;
	private JSeparator separator;
	private JTable table;
	
	int rowSelected;
	DefaultTableModel model;
	private ArrayList<String> Requests = new ArrayList<String>();
	private ArrayList<String> Extend = new ArrayList<String>();
	
	/**
	 * Create the panel.
	 * @param Requests 
	 */
	public AdminReqPanel(JPanel contentPane, ArrayList<String> Req) {
		this.jp = contentPane;
		this.Requests = Req;
		setLayout(null);
		initialize();
	}
	
	/**
	 * Initialize.
	 */
	private void initialize(){
		setLayout(null);
			
		l = new JLabel("Requests");
		l.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l.setBounds(273, 11, 111, 29);
		add(l);
		
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
				
		String col[] = {"RequestId","TID","ExamId","RequestTime","Request"};
		String data[][] = {{}};
		model = new DefaultTableModel(data,col);
		
		table = new JTable(model);
		table.setFont(new Font("Tahoma", Font.BOLD, 12));
		table.setBounds(10, 75, 607, 284);
		add(table);
		
		int j = 1;
		model.insertRow(0,new Object[]{"Request ID","Teacher ID","Exam ID","Requested Time","Comments"});
		for(int i=0;i<Requests.size();i++,j++)
			model.insertRow(j,new Object[]{Requests.get(i),Requests.get(++i),Requests.get(++i),Requests.get(++i),Requests.get(++i)});
		model.removeRow(j);
		
		b2 = new JButton("Cancel");
		b2.setBounds(369, 391, 91, 23);
		add(b2);
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Requests.clear();
				CardLayout cl = (CardLayout)(jp.getLayout());
				cl.show(jp,"AdminMain");	
			}
		});
		
		b3 = new JButton("Approve");
		b3.setBounds(275, 391, 89, 23);
		add(b3);
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rowSelected = table.getSelectedRow();
				if(rowSelected > 0 && rowSelected <= Requests.size()/5){
				Extend.clear();
//				System.out.printf("selected row: %d\n",rowSelected);
//				System.out.printf("selected exam : %s time: %s\n",Requests.get(2+((rowSelected-1)*5)),Requests.get(3+((rowSelected-1)*5)));
//				System.out.printf("\nRequest id: %s",Requests.get(((rowSelected-1)*5)));
				Extend.add(Requests.get((rowSelected-1)*5));
				Extend.add(Requests.get(2+((rowSelected-1)*5)));
				Extend.add(Requests.get(3+((rowSelected-1)*5)));
				AdminControl.setNewExamTime(Extend);
		
				model.removeRow(rowSelected);
				JOptionPane.showMessageDialog(null,"Exam Time Has Been Changed!", "Done!",JOptionPane.INFORMATION_MESSAGE);
				}
			}	
		});
		 
		b4 = new JButton("Decline");
		b4.setBounds(180, 391, 89, 23);
		add(b4);
		b4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rowSelected = table.getSelectedRow();
				if(rowSelected > 0 && rowSelected <= Requests.size()/5){
				Extend.clear();
//				System.out.printf("request ID: %s selected exam : %s time: %s\n",Requests.get(((rowSelected-1)*5)),Requests.get(2+((rowSelected-1)*5)),Requests.get(3+((rowSelected-1)*5)));	
				Extend.add(Requests.get((rowSelected-1)*5));
				Extend.add("none");
				AdminControl.setNewExamTime(Extend);
				
				model.removeRow(rowSelected);
				}
			}
		});	
	}
}
